package com.ly.honor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.honor.entity.News;
import com.ly.honor.entity.dto.NewsCreateDTO;
import com.ly.honor.entity.dto.NewsEditDTO;
import com.ly.honor.entity.dto.NewsQueryDTO;
import com.ly.honor.entity.vo.NewsQueryVO;
import com.ly.honor.exception.HonorException;
import com.ly.honor.service.NewsService;
import com.ly.honor.mapper.NewsMapper;
import com.ly.honor.util.HonorUtil;
import com.ly.honor.util.ResultEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
* @author 17798
* @description 针对表【bs_news】的数据库操作Service实现
* @createDate 2022-11-16 11:49:49
*/
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News>
    implements NewsService{

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private HonorUtil honorUtil;

    @Override
    public Page<NewsQueryVO> getNewsPageList(long pageNum, long pageSize, NewsQueryDTO newsDTO) {
        Page<NewsQueryVO> newsPage = new Page<>(pageNum, pageSize);

        Page<NewsQueryVO> newsQueryVOPage = newsMapper.getNewsPageList(newsDTO,newsPage);
        return newsQueryVOPage;
    }

    public Page<NewsQueryVO> getNewsPageListDesc(long pageNum, long pageSize) {
        Page<NewsQueryVO> newsPage = new Page<>(pageNum, pageSize);

        Page<NewsQueryVO> newsQueryVOPage = newsMapper.getNewsPageListDesc(newsPage);
        return newsQueryVOPage;
    }

    @Override
    public String uploadVideo(MultipartFile file) {
        var url = honorUtil.uploadVideo(file);
        return url;
    }

    @Override
    public NewsQueryVO getNewsById(long id) {
        LambdaQueryWrapper<News> wrapper = new LambdaQueryWrapper<News>();
        wrapper.eq(News::getId,id);
        News news = newsMapper.selectOne(wrapper);
        if(news == null){
            return null;
        }
        NewsQueryVO newsQueryVO = new NewsQueryVO();
        BeanUtils.copyProperties(news,newsQueryVO);
        return newsQueryVO;

    }

    @Override
    public void insertNews(NewsCreateDTO newsCreateDTO) {
        News news = new News();
        if(ObjectUtils.isEmpty(newsCreateDTO)){
            throw new HonorException(ResultEnum.OBJECT_NOT_NULL);
        }
        BeanUtils.copyProperties(newsCreateDTO,news);
        if(news.getPicture() != null && news.getPicture().length() != 0){
            news.setPicture(news.getPicture().split("\\?")[0]);
        }
        if(news.getVideo() != null && news.getVideo().length() != 0){
            news.setVideo(news.getVideo().split("\\?")[0]);
        }
        newsMapper.insert(news);
    }

    @Override
    public void updateNewsById(NewsEditDTO newsEditDTO) {
        if(ObjectUtils.isEmpty(newsEditDTO)){
            throw new HonorException(ResultEnum.OBJECT_NOT_NULL);
        }
        News news = new News();
        BeanUtils.copyProperties(newsEditDTO,news);
        if(news.getPicture() != null && news.getPicture().length() != 0){
            news.setPicture(news.getPicture().split("\\?")[0]);
        }
        if(news.getVideo() != null && news.getVideo().length() != 0){
            news.setVideo(news.getVideo().split("\\?")[0]);
        }
        newsMapper.updateById(news);
    }

    @Override
    public String upload(MultipartFile file) {
        var url = honorUtil.uploadFile(file);
        System.out.println(url);
        return url;
    }

    @Override
    public List<NewsQueryVO> recentlyHighHeatNews() {
        List<NewsQueryVO> newsQueryVOList = newsMapper.recentlyHighHeatNews();
        return newsQueryVOList;
    }

    @Override
    public void newsHeatRaise(Integer id) {
        var news = newsMapper.selectById(id);
        news.setHeat(news.getHeat() + 1);
        newsMapper.updateById(news);
    }
}




